﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Jayden.Dll.Cards.Poker;

namespace Jayden.Windows.PokerClient
{
	public partial class TournamentLobbyForm : Form
	{

		public string TournamentName;
		public WindowsPokerClient Client;
		public bool IsRegistered;
		private System.Timers.Timer m_Timer = new System.Timers.Timer();

		public TournamentLobbyForm()
		{
			InitializeComponent();

			m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshLobby);
			m_Timer.Interval = 5000;
			m_Timer.Enabled = true;
		}

		private void RefreshLobby(object sender, System.Timers.ElapsedEventArgs e)
		{
			if (Visible == false || WindowState != FormWindowState.Minimized)
			{
				if (Client != null)
				{
					Client.TournamentLobbyPlayers(TournamentName);
					Client.TournamentLobbyPayoutStructure(TournamentName);
				}
			}
		}
		public void Init()
		{
			if (IsRegistered)
				bRegister.Text = "Unregister";
			else
			{
				bRegister.Text = "Register";
				if (string.IsNullOrWhiteSpace(Client.Login))
					bRegister.Enabled = false;
			}
		}

		private void bRegister_Click(object sender, EventArgs e)
		{
			if (IsRegistered)
				Client.Unregister(TournamentName);
			else
				Client.Register(TournamentName);
			Client.TournamentLobbyPlayers(TournamentName);
			Client.TournamentLobbyPayoutStructure(TournamentName);
		}

		protected override void OnClosed(EventArgs e)
		{
			base.OnClosed(e);
			m_Timer.Enabled = false;
			Client.LeaveTournamentLobby(TournamentName);
		}

		public void SetPlayers(List<KeyValuePair<string, ulong>> players)
		{
			lPlayers.BeginUpdate();
			lPlayers.Items.Clear();
			players.Sort(delegate(KeyValuePair<string, ulong> a, KeyValuePair<string, ulong> b)
			{
				int compare = -a.Value.CompareTo(b.Value);
				if (compare == 0)
					return a.Key.CompareTo(b.Key);
				return compare;
			});
			int position = 1;
			foreach (KeyValuePair<string, ulong> pair in players)
			{
				string format = "{0} finished {2}";
				if(pair.Value > 0)
					format = "{0} ({1})";
				lPlayers.Items.Add(string.Format(format, pair.Key, pair.Value, position));
				position++;
			}
			lPlayers.EndUpdate();
		}

		public void SetBettingStructure(List<PokerTournamentConfiguration.BettingStructureEntry> bettingStructure)
		{
			lBetting.BeginUpdate();
			lBetting.Items.Clear();
			foreach (PokerTournamentConfiguration.BettingStructureEntry entry in bettingStructure)
			{
				string format = "{0}/{1} with ante {2} ({3} min.)";
				if (entry.Ante == 0)
					format = "{0}/{1} ({3} min.)";
				lBetting.Items.Add(string.Format(format, entry.SmallBlind, entry.BigBlind, entry.Ante, (entry.Duration / 60000)));
			}
			lBetting.EndUpdate();
		}

		public void SetPayoutStructure(List<PokerTournamentConfiguration.PayoutEntry> payoutStructure)
		{
			lPayout.BeginUpdate();
			lPayout.Items.Clear();
			foreach (PokerTournamentConfiguration.PayoutEntry entry in payoutStructure)
			{
				string format = "{0} to {1} -> {2}";
				if (entry.FirstPosition == entry.LastPosition)
					format = "{0} -> {2}";
				lPayout.Items.Add(string.Format(format, entry.FirstPosition + 1, entry.LastPosition + 1, PokerCurrency.EUR.ToString(entry.Amount)));
			}
			lPayout.EndUpdate();
		}
	}
}
